package hz.mobileproject.pda.ui.frgment.work;

import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Bundle;
import android.util.Log;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.TimeUtils;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import cn.droidlover.xdroidmvp.mvp.XLazyFragment;
import hz.mobileproject.pda.R;
import hz.mobileproject.pda.broadcast.HandleBroadcast;
import hz.mobileproject.pda.broadcast.IShowBarcodeStr;
import hz.mobileproject.pda.db.DBManager;
import hz.mobileproject.pda.entity.request.Request_Callroll_loadCnf;
import hz.mobileproject.pda.entity.request.Request_loadPoliceInfo;
import hz.mobileproject.pda.entity.request.Request_loadPrisonerList;
import hz.mobileproject.pda.entity.request.Request_loadSubPrison;
import hz.mobileproject.pda.entity.request.Request_start;
import hz.mobileproject.pda.entity.response.Response_loadCnf;
import hz.mobileproject.pda.entity.response.Response_loadPoliceInfo;
import hz.mobileproject.pda.entity.response.Response_loadSubPrison;
import hz.mobileproject.pda.entity.response.Response_start;
import hz.mobileproject.pda.callroll.presenter.PCalroll;
import hz.mobileproject.pda.ui.view.LoadingDialog;
import hz.mobileproject.pda.utils.IsOrNo_Area;

/**
 * Created by Administrator on 2017/7/11 0011.
 */

public class CallrollFragment extends XLazyFragment<PCalroll> implements IShowBarcodeStr {

    private String cusNumber;//机构号
    private String Poilce_id;
    private Boolean On_off;

    public static final String LoadCnf = "loadCnf";
    private HandleBroadcast handleBroadcast;
    private SoundPool soundpool;
    private int soundid;
    private Gson gson;
    private String callroll_data;
    private SPUtils spUtils;
    private SPUtils spUtils_cusNumber;
    private DBManager dBManager;
    private LoadingDialog loadingDialog;
    private Response_loadPoliceInfo loadPoliceInfo;
    private Response_loadCnf response_loadCnf;
    private Response_start response_start;


    public static CallrollFragment newInstance() {

        Bundle args = new Bundle();
        CallrollFragment fragment = new CallrollFragment();
        fragment.setArguments(args);
        return fragment;
    }
    @Override
    public int getLayoutId() {
        return R.layout.activity_callroll;
    }

    @Override
    public PCalroll newP() {
        return new PCalroll(getActivity());
    }


    @Override
    public void initData(Bundle savedInstanceState) {

        //对本地数据库的操作
        dBManager= DBManager.getInstance(getContext());
        gson = new Gson();

        spUtils = SPUtils.getInstance("Callroll_loadCnf");
        spUtils_cusNumber=SPUtils.getInstance("app_login");
        cusNumber= spUtils_cusNumber.getString("cusnumber");
//获取配置
        Request_Callroll_loadCnf callroll_loadCnf= new Request_Callroll_loadCnf();
        String nowTime=TimeUtils.getNowString(new SimpleDateFormat("yyyy-MM-dd"));
        callroll_loadCnf.setCusNumber(cusNumber);
        callroll_loadCnf.setDate(nowTime);
        callroll_loadCnf.setTime(TimeUtils.getNowString());
        callroll_data=  gson.toJson(callroll_loadCnf);
        getP().getData(callroll_data,4);
        spUtils.put("loadCnf",nowTime);

        On_off=true;
        LogUtils.e(spUtils_cusNumber);

        if(!spUtils.getString("loadCnf").equals(TimeUtils.getNowString(new SimpleDateFormat("yyyy-MM-dd")))){

        }

        //初始化广播
        initScan();

    }
    private void initScan() {
        handleBroadcast = new HandleBroadcast(this,getContext());
        soundpool = new SoundPool(1, AudioManager.STREAM_NOTIFICATION, 100); // MODE_RINGTONE
        soundid = soundpool.load("/etc/Scan_new.ogg", 1);
    }


    @Override
    protected void onResumeLazy() {
        super.onResumeLazy();
        //开启广播接收扫描
        try {
            handleBroadcast.sendMyBroadcast();
            showPromptDlg();
        }catch (Exception e){
            Log.e(e.getMessage(),e.getMessage());
        }
    }

    private void showPromptDlg() {
        loadingDialog=  new LoadingDialog(getContext());
        loadingDialog.setMessage("请扫描民警二维码").show();
    }

    @Override
    public void updateText(String msg) {
        Log.e("扫描传过来的",msg);
        soundpool.play(soundid, 1, 1, 0, 0, 1);
//判断传递过来的数据是否是民警      //机构号

        boolean status = msg.contains(cusNumber);
        if(status&&On_off){
//获取民警基本信息
            Poilce_id=msg;
            Request_loadPoliceInfo callroll_loadPoliceInfo= new Request_loadPoliceInfo();
            callroll_loadPoliceInfo.setCusNumber("3223");
            callroll_loadPoliceInfo.setPoliceCode("3304129");  //3304281  一监区
            callroll_data=  gson.toJson(callroll_loadPoliceInfo);
            getP().getData(callroll_data,1);
        }else {

        }

        Poilce_id=msg;
        Request_loadPoliceInfo callroll_loadPoliceInfo= new Request_loadPoliceInfo();
        callroll_loadPoliceInfo.setCusNumber("3223");
        callroll_loadPoliceInfo.setPoliceCode("3304129");  //3304281  一监区
        callroll_data=  gson.toJson(callroll_loadPoliceInfo);
        getP().getData(callroll_data,1);

    }

    //根据服务端的数据做不同操作
    public void respnose_data(String data_json,int type){
        switch (type){
            case 4:  //获取的是批次
                //在点名时间
                if (data_json.equals("不在点名时间")){
                    loadingDialog.setMessage("不在点名时间").show();
                    On_off=false;
                }else {
                    response_loadCnf= gson.fromJson(data_json,Response_loadCnf.class);
                }

                break;
            case 1://民警基本信息获取

                loadPoliceInfo= gson.fromJson(data_json,Response_loadPoliceInfo.class);

                send_start();
                dBManager.insertloadPoliceInfo(loadPoliceInfo);

                //判断是否是分监区民警
                if(IsOrNo_Area.getInstance().getArea(loadPoliceInfo.getDepartment_name())){
                    // 2.1.2	分监区查询
                    Request_loadSubPrison request_loadSubPrison= new Request_loadSubPrison();
                    request_loadSubPrison.setCusNumber("3223");
                    request_loadSubPrison.setDepartmentId(String.valueOf(loadPoliceInfo.getDepartment_id()));
                    callroll_data=  gson.toJson(request_loadSubPrison);
                    getP().getData(callroll_data,2);


                }else {
                    //2.1.3	根据部门编号获取需要点名的罪犯列表

                    Request_loadPrisonerList request_loadPrisonerList=   new Request_loadPrisonerList();
                    request_loadPrisonerList.setCusNumber("3223");
                    request_loadPrisonerList.setDepartmentId("54");
                    callroll_data=gson.toJson(request_loadPrisonerList);
                    getP().getData(callroll_data,3);

                }

                break;
            case 2:
                List<Response_loadSubPrison> loadSubPrisons= gson.fromJson(data_json, new TypeToken<List<Response_loadSubPrison>>(){}.getType());
                showWheelView(loadSubPrisons);
                break;
            case 6:   //开始点名
                response_start= gson.fromJson(data_json,Response_start.class);

                break;

        }

    }

    //非分监区民警 提示显示哪个监区，让其选择
    private void showWheelView(List<Response_loadSubPrison> data) {

        List<String> strings = new ArrayList<>();

        for (int i=0;i<data.size();i++){
            strings.add(data.get(i).getDepartment_name()+"-"+data.get(i).getDepartment_id());
        }

        /*DialogUIUtils.showCustomBottomAlert()*/
        send_start();
    }

    //开始点名
    public void send_start(){

        Request_start request_start=  new Request_start();

        request_start.setCusNumber(cusNumber);//机构编号
        request_start.setBatchCode(String.valueOf(response_loadCnf.getBatch_code()));//点名批次编号
        request_start.setDepartmentId(String.valueOf(loadPoliceInfo.getDepartment_id()));//部门编号
        request_start.setDepartmentName(loadPoliceInfo.getDepartment_name());//部门名称
        request_start.setTime(TimeUtils.getNowString());//点名时间

        callroll_data=  gson.toJson(request_start);
        getP().getData(callroll_data,6);

    }


}
